package t.yatush.core_infra_svc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public abstract class DbSysTool {

	private static final String DB_URL = "jdbc:postgresql://yatush.no-ip.org";
	private static final String USER_NAME = "postgres";
	private static final String PASSWORD = "postgres";
	protected Connection con;

	protected DbSysTool() {
		System.out.println("-------- PostgreSQL "
				+ "JDBC Connection Testing ------------");

		try {

			Class.forName("org.postgresql.Driver");

		} catch (ClassNotFoundException e) {

			System.out.println("Where is your PostgreSQL JDBC Driver? "
					+ "Include in your library path!");
			e.printStackTrace();
			return;

		}

		System.out.println("PostgreSQL JDBC Driver Registered!");

		Connection connection = null;

		try {
			connection = DriverManager.getConnection(DB_URL, USER_NAME,
					PASSWORD);

		} catch (SQLException e) {

			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
			return;

		}

		if (connection != null) {
			System.out.println("You made it, take control your database now!");
			con = connection;
		} else {
			System.out.println("Failed to make connection!");
		}
	}
	
	public void runStrScript(String scriptStr) throws Exception {
		for (String stmt : scriptStr.split(";")) {
				PreparedStatement statement = con.prepareStatement(stmt);
				statement.execute();
				statement.close();
		}
	}


}
